package com.easytouch.vmhdl.view;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by Administrator on 2018/1/17.
 */

public class RipperDrawable extends Drawable {
	// Drawable透明度0-255
	private int mAlpha = 255;
	private int mRipperColor = 0;

	private float mRipperPointX, mRipperPointY;
	private float mRipperRadius = 0;

	// 画笔
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

	public RipperDrawable() {
		// 抗锯齿
		mPaint.setAntiAlias(true);
		// 抗抖动
		mPaint.setDither(true);

		setRipperColor(0x30000000);

		// 滤镜 ARGB
		setColorFilter(new LightingColorFilter(0xFFFF0000, 0x00330000));
	}

	private int getCircleAlpha(int preAlpha, int bgAlpha) {
		int dAlpha = preAlpha - bgAlpha;
		return (int) ((dAlpha * 255f) / (255f - bgAlpha));
	}

	private int mBgAlpha;
	private int mCircleAlpha;

	@Override
	public void draw(@NonNull Canvas canvas) {

		int preAlpha = mPaint.getAlpha();
		int bgAlpha = (int) (preAlpha * (mBgAlpha / 255f));
		int maxCircleAlpha = getCircleAlpha(preAlpha, bgAlpha);
		int circleAlpha = (int) (maxCircleAlpha * (mCircleAlpha / 255f));
		// 绘制背景区域颜色
		mPaint.setAlpha(bgAlpha);
		// 画上一个颜色
		canvas.drawColor(mPaint.getColor());

		// 画上一个圆
		mPaint.setAlpha(circleAlpha);
		canvas.drawCircle(mRipperPointX, mRipperPointY, mRipperRadius, mPaint);
		
		mPaint.setAlpha(preAlpha);

	}

	public void setRipperColor(int color) {
		// 不建议
		this.mRipperColor = color;
		onColorOrAlphaChange();
		// 刷新当前Drawable
		invalidateSelf();
	}

	private void onColorOrAlphaChange() {
		mPaint.setColor(mRipperColor);
		if (mAlpha != 255) {
			int pAlpha = mPaint.getAlpha();
			int realAlpha = (int) (pAlpha * (mAlpha / 255f));
			mPaint.setAlpha(realAlpha);
		}

	}

	// 标识用户手是否抬起
	private boolean onTouchRelease;

	public void onTouch(MotionEvent event) {
		invalidateSelf();
		switch (event.getActionMasked()) {
		case MotionEvent.ACTION_DOWN:
			onTouchDown(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_MOVE:
			onTouchMove(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_UP:
			onTouchUp(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_CANCEL:
			onTouchCancel(event.getX(), event.getY());
			break;
		}
	}

	private void onTouchDown(float x, float y) {
		onTouchRelease = false;
		mDonePointX = x;
		mDonePointY = y;
		startEnterRunnable();
	}

	private void onTouchMove(float x, float y) {
	}

	private void onTouchUp(float x, float y) {
		onTouchRelease = true;
		// 启动退出动画
		if (mEnterDone)
			startExitRunnable();
	}

	private void onTouchCancel(float x, float y) {
		onTouchRelease = true;
		// 启动退出动画
		if (mEnterDone)
			startExitRunnable();
	}

	// 启动进入动画的方法
	private void startEnterRunnable() {
		mCircleAlpha = 255;

		mEnterDone = false;
		mEnterProgress = 0;
		unscheduleSelf(mExitRunnable);
		unscheduleSelf(mRunnable);
		scheduleSelf(mRunnable, SystemClock.uptimeMillis());
	}

	// 启动退出动画的应运
	private void startExitRunnable() {
		mExitProgress = 0;
		unscheduleSelf(mRunnable);
		unscheduleSelf(mExitRunnable);
		scheduleSelf(mExitRunnable, SystemClock.uptimeMillis());
	}

	private boolean mEnterDone;
	// 进入动画进度值
	private float mEnterProgress = 0;
	// 递增值
	private float mEnterIncrement = 16f / 180;
	// 进入动画查值器，用于实现从快到慢的效果
	private DecelerateInterpolator mInterpolator = new DecelerateInterpolator(5);
	// 动画的回调
	private Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			mEnterProgress = mEnterProgress + mEnterIncrement;
			if (mEnterProgress > 1) {
				onEnterProgress(1);
				onEnterDone();
				return;
			}
			float realProgress = mInterpolator.getInterpolation(mEnterProgress);
			onEnterProgress(realProgress);
			// 延迟16毫秒，保证界面刷新频率接近60fps
			scheduleSelf(this, SystemClock.uptimeMillis() + 16);
		}
	};

	private void onEnterProgress(float progress) {
		mRipperRadius = getProgressValue(mStartRadius, mEndRadius, progress);
		mRipperPointX = getProgressValue(mDonePointX, mCenterPointX, progress);
		mRipperPointY = getProgressValue(mDonePointY, mCenterPointY, progress);

		mBgAlpha = (int) getProgressValue(0, 182, progress);
		invalidateSelf();
	}

	// 退出动画进度值
	private float mExitProgress = 0;
	// 递增值
	private float mExitIncrement = 16f / 150;
	// 退出动画查值器，用于实现从快到慢的效果
	private AccelerateInterpolator mExitInterpolator = new AccelerateInterpolator(
			5);
	// 动画的回调
	private Runnable mExitRunnable = new Runnable() { 
		@Override
		public void run() {
			//进入时，首先判断进入动画是否具有
			if(!mEnterDone)
				return;
			
			mExitProgress = mExitProgress + mExitIncrement;
			if (mExitProgress > 1) {
				onExitProgress(1);
				onExitDone();
				return;
			}
			float realProgress = mExitInterpolator
					.getInterpolation(mExitProgress);
			onExitProgress(realProgress);
			// 延迟16毫秒，保证界面刷新频率接近60fps
			scheduleSelf(this, SystemClock.uptimeMillis() + 16);
		}
	};

	// 退出动画刷新
	private void onExitProgress(float progress) {
		mBgAlpha = (int) getProgressValue(182, 0, progress);//背景减淡
		mCircleAlpha = (int) getProgressValue(255, 0, progress);//圆形减淡
		invalidateSelf();
	}

	private void onExitDone() {
	}

	private void onEnterDone() {
		mEnterDone = true;
		if (onTouchRelease) {
			startExitRunnable();
		}

	}

	private float getProgressValue(float start, float end, float progress) {
		return start + (end - start) * progress;
	}

	// 按下时的点击点
	private float mDonePointX, mDonePointY;
	// 控件的中心区域点
	private float mCenterPointX, mCenterPointY;
	// 开始和结束的半径
	private float mStartRadius, mEndRadius;

	// 更改背景颜色透明度的方法
	private int changeColorAlpha(int color, int alpha) {
		int a = (color >> 24) & 0xFF;
		a = (int) (a * (alpha / 255f));
		int r = (color >> 16) & 0xFF;
		int g = (color >> 8) & 0xFF;
		int b = (color) & 0xFF;
		return (a << 24) | (r << 16) | (g << 8) | b;
	}

	@Override
	protected void onBoundsChange(Rect bounds) {
		super.onBoundsChange(bounds);
		mCenterPointX = bounds.centerX();
		mCenterPointY = bounds.centerY();

		// 得到最大的半径
		float maxRadius = Math.max(mCenterPointX, mCenterPointY);
		mStartRadius = maxRadius * 0f;
		mEndRadius = maxRadius * 0.8f;
	}

	@Override
	public void setAlpha(int alpha) {
		// 配置Drawable的透明度
		this.mAlpha = alpha;
		onColorOrAlphaChange();
		invalidateSelf();
	}

	@Override
	public int getAlpha() {
		return mAlpha;
	}

	@Override
	public void setColorFilter(@Nullable ColorFilter colorFilter) {
		if (mPaint.getColorFilter() != colorFilter) {
			mPaint.setColorFilter(colorFilter);
			invalidateSelf();
		}
	}

	@Override
	public int getOpacity() {
		int alpha = mPaint.getAlpha();
		if (alpha == 255) {// 不透明
			return PixelFormat.OPAQUE;
		} else if (alpha == 0) {// 全透明
			return PixelFormat.TRANSPARENT;
		} else {// 半透明
			return PixelFormat.TRANSLUCENT;
		}
	}
}
